<template>
    <div class="execution">
        <basic-container>
            <avue-crud
                       :permission="permissionList"
                       v-model="crudForm"
                       :before-open="beforeOpen"
                       v-bind="bindVal"
                       v-on="onEvent"
                       :page.sync="page"
            >
                      <template slot="menuLeft">
                        <el-button type="primary"
                                    size="small"
                                    v-show="permissionList.submitBtn"
                                    @click.stop="handleSubmit()">提交
                        </el-button>
                        <el-button type="primary" size="small" @click="delBatch">删除</el-button>
                      <!-- <el-upload
                      class='upload-inline'
                      action=''
                      :show-file-list="false"
                      accept="xsl,xsls"
                      :http-request="handleImport"
                      >
                      <el-button type='primary' size='small'>导入</el-button>
                      </el-upload> -->
                      <el-button type="primary"
                                  size="small"
                                  @click.stop="handleExport()">导出
                      </el-button>
                      </template>
                      <template slot="menu" slot-scope="{row, size}">
                        <el-button
                          v-if="permissionList.editBtn && (row.submitStatus=='0')"
                          type="text"
                          :size="size"
                          icon="el-icon-edit"
                          @click="handleEdit(row)">编辑
                        </el-button>
                      </template>

            </avue-crud>
        </basic-container>
    </div>
</template>

<script>
    import {fetchList, getObj, addObj, putObj, importObj, exportObj, delObj, submit,delBatchObj,exportExcel} from '@/api/subwaystm/swstspecialcardapply'
    import {tableOption} from '@/const/crud/subwaystm/swstspecialcardapply'
    import {dateFormat} from '@/util/date'
    import {handleExcelResponse} from '@/util/util'
    import {mapGetters} from 'vuex'
    import baseCrud from '@/mixins/base-crud'

    export default {
        name: 'swstspecialcardapply',
      mixins: [baseCrud('subwaystm/swstspecialcardapply')],
        data() {
            return {
                searchForm: {
                  submitStatus: '0'
                },
                crudForm: {},
                form: {
                  type: undefined,
                  dictId: undefined
                },
                dictType: undefined,
                dictId: undefined,
                dialogFormVisible: false,
                importBtn: true,
                excelUrl: "/stm/excel/a.xlsx",
                enabledUploadBtn: true,
                fileList: [],
                uploadBtnIcon: 'el-icon-upload2',
                tableData: [],
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.stm_sw_st_special_card_apply_add, false),
                    delBtn: this.vaildData(this.permissions.stm_sw_st_special_card_apply_del, false),
                    editBtn: this.vaildData(this.permissions.stm_sw_st_special_card_apply_edit, false),
                    submitBtn: this.vaildData(this.permissions.stm_sw_st_special_card_apply_submit, false),
                    // exportBtn: this.vaildData(this.permissions.stm_sw_st_special_card_apply_export, false)
                };
            }
        },
        methods: {
            async handleExport(){
              let loading = this.$loading({
                lock: true,
                text: "Loading",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)"
              })
              try {
                let tableSelect = this.$refs.crud.tableSelect;
                let ids = tableSelect.map(row => row.id)
                let param = {
                  ids: this.validatenull(ids)?null:ids
                };
                const res = await exportExcel(param);
                handleExcelResponse(res);
              } finally {
                loading.close();
              }
            },
            handleEdit(row){
              this.$refs.crud.rowEdit(row)
            },
            delBatch: function () {
              if (this.$refs.crud.tableSelect.length < 1) {
                this.$message.error("请勾选数据");
                return false;
              }
              let ids = [];
              for (const table of this.$refs.crud.tableSelect) {
                if (table.submitStatus != "0") {
                  this.$message.error("请检查数据，只能删除未提交的数据");
                  return false;
                }
                ids.push(table.id);
              }
              this.$confirm("确认删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              }).then(() => {
                delBatchObj(ids)
                  .then((data) => {
                    this.$message.success("删除成功");
                    this.getList(this.page);
                    done();
                  })
                  .catch(() => {
                    loading();
                  });
              });  
            },
            beforeOpen(done, type){
              if(type == 'add'){
                setTimeout(()=>{
                  this.crudForm.applyTime = dateFormat(new Date())
                  // let today = dateFormat(new Date()).substr(0, 10) + ' 00:00:00'
                  // this.form.planStartTime = today
                },0)
              }
              done()
            },
            handleImport(item) {
                let file = item.file;
                let params = new FormData();
                params.append("file",file);
                  importObj(params).then(() => {
                    this.$notify({
                    title: '成功',
                    message: '导入成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.getList(this.page);
                })
            },
            doImport() {
              var that = this;
              setTimeout(function () {
                if (that.$refs.upload.$children[0].fileList.length == 1) {
                  that.$refs.upload.submit();
                } else {
                  that.$message({
                    type: 'error',
                    showClose: true,
                    duration: 3000,
                    message: '请选择文件!'
                  });
                }
                ;
              }, 100);
            },
            downloadFun() {
              location.href = "http://www.zhudaokeji.com:8007/subwaystm/%E7%89%B9%E6%AE%8A%E5%B7%A5%E4%BD%9C%E5%8D%A1%E7%94%B3%E8%AF%B7%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20200131%2F%2Fs3%2Faws4_request&X-Amz-Date=20200131T114548Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=d7f6487542047aba363e31acd7a29dd9e9bbf2b6477100eed6cecc5712e410ae";
            },
            handleSubmit(){
              var tableSelect = this.$refs.crud.tableSelect
              if (tableSelect.length == 0) {
                this.$message.info("请选择要提交的记录");
                return false;
              }
              for (const table of tableSelect) {
                if(table.submitStatus !=0){
                  this.$message.error("已提交的数据不能重复提交!");
                  return false;
                }
              }
              var ids = tableSelect.map(row => row.id)
              submit(ids).then(data => {
                this.$message.success("提交成功");
                this.getList(this.page);
              })
            }
    }
}
</script>
